package com.bosicen.crm.service.orders;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bosicen.crm.dao.entity.Dealer;
import com.bosicen.crm.dao.entity.Order;
import com.bosicen.crm.dao.entity.OrderDetail;
import com.bosicen.crm.dao.orders.OrdersDao;
import com.bosicen.crm.dao.sysmanage.DealerDao;
import com.bosicen.crm.service.CommonServiceImpl;
import com.bosicen.crm.tools.Page;

/***
 * 订单操作业务层实现
 * @author Administrator
 *
 */
@Service("ordersservice")
public class OrdersServiceImpl {

	private static Logger logger = Logger.getLogger(OrdersServiceImpl.class);    
	@Autowired
	private OrdersDao ordersdao;
	@Autowired
	private DealerDao dealerDao;
	@Autowired
	CommonServiceImpl commonService;
	/***
	 * 获取当天订单数
	 * @return
	 */
	public int findOrders4day(){
		return this.ordersdao.findOrders4day();
	}
	
	/****
	 * 查询单个详细订单信息
	 * @param id 订单详细ID
	 * @return
	 */
	public OrderDetail findOrderDetail(int id){
		return this.ordersdao.findOrderDetail(id);
	}
	/****
	 * 删除单个订单详细信息
	 * @param id
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean delOrderDetail(int id){
		try{
			int total=this.ordersdao.delOrderDetail(id);
			if(total>0)
				return true;
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return false;
	}
	/***
	 * 修改单个订单信息
	 * @param od
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean updateOrderDetail(OrderDetail od){
		
		try{
			this.ordersdao.updateOrderDetail(od);
			return true;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return false;
		
	}
	/***
	 * 订单查询信息
	 * @param order
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page findOrder(Order order,Page page){
		page=this.ordersdao.findOrder(order, page);
		if(page!=null){
			List<Order> list=page.getResult();
			for(Order o:list){//初始化信息
				
			}
		}
		return null;
		
	}
	/****
	 * 新增修改订单
	 * @param order
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveOrUpdateOrders(Order order){
		try{
			this.ordersdao.saveOrUpdate(order);
		}catch(Exception ex){
			ex.printStackTrace();
			logger.error("新增订单抛出异常："+ex);
			return false;
		}
		return true;
	}
	/****
	 * 通过订单id 获取订单详细
	 * @param orderid
	 * @return
	 */
	public Order findOrderById(int orderid){
		Order order=this.ordersdao.findOrderById(orderid);
		if(order!=null){
			Dealer dealer=this.dealerDao.findDealerById(order.getSource());
			order.setSourcename(dealer.getName());
		}
		return order;
	}
	/****8
	 * 新增订单详细
	 * @param orderid
	 * @param detail
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveOrderDetails(int orderid,OrderDetail detail){
		Order order=this.ordersdao.get(orderid);
		detail.setOrder(order);
		order.getOrderdetail().add(detail);
		return this.saveOrUpdateOrders(order);
	}
	
	/****
	 * 查询详细产品数量
	 * @param pid
	 * @return
	 */
	public 	List<Object[]> findOrderDetailByType(int pid){
		return this.ordersdao.findOrderDetailByType(pid);
	}
}
